/*
 * @Author: 陈明烽
 * @FilePath: /umi-ssr/src/utils/cookie.ts
 * @Description: cookie操作
 */


import { isBrowser } from 'umi';



const setCookie = (name:string, value:string, days?:number, path?:string) => {
  if (!isBrowser()) { return }
  const Days = days || 30;
  const exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  if (path) {
    document.cookie = `${name}=${encodeURIComponent(value)};path=${path};expires=${exp.toString()}`;
  } else {
    document.cookie = `${name}=${encodeURIComponent(value)};expires=${exp.toString()}`;
  }
}

const getCookie = (name:string) => {
  let cookie
  if (!isBrowser()) {
    // 这里需要在服务端处理好cookie，处理成{key:value}
    // @ts-ignore
    cookie = global._cookies[name] || null
  } else {
    const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
    const arr = document.cookie.match(reg);
    if (arr) { cookie = decodeURIComponent(arr[2]); }
  }
  return cookie
}


export {
  setCookie,
  getCookie,
};

